﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Bussines;
using System.Data.SqlClient;

namespace ownx
{
    public partial class wfUsers : System.Web.UI.Page
    {
        clUser _clUser;
        DataSet dsUsers;
        DataSet ds;
        TBLObj blObj = null;

        #region Page load, datainit
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (Session["BL"] != null)
            {
                blObj = (TBLObj)Session["BL"];
            }
            //TBLObj blObj = new TBLObj("Test", Session["CONSTR"].ToString(), (clFIELDINFO_LIST)Session["FIELDINFO"]);
            _clUser = new clUser(blObj);

            dsUsers = _clUser.GetUsers();

            //ha ez az első meghívás
            if (!(Page.IsPostBack))
            {
                DataInit();
            }
        }

        private void DataInit()
        {
            //feltöltöm a detasetet, majd a gridet
            dsUsers = _clUser.GetUsers();

            gvUser.DataSource = dsUsers;
            gvUser.DataBind();

            //feltötlöm a főnök combot, és kiválasztom a főnököt
            ds = new DataSet();
            ds = _clUser.GetUsers();
            ddlUSR_FONOK.Items.Clear();
            for (int i = 0; i < ds.Tables["_USER"].Rows.Count; ++i)
            {
                ListItem li = new ListItem(ds.Tables["_USER"].Rows[i]["USR_NAME"].ToString(), ds.Tables["_USER"].Rows[i]["USR_ID"].ToString());
                ddlUSR_FONOK.Items.Add(li);
            }
            //kijelölöm az elsőt, és kitöltöm az editeket
            gvUser.SelectedIndex = 0;
            gvUser_SelectedIndexChanged(gvUser, null);
        }
        #endregion

        #region Új, Adatmentés, Törlés

        private void Uj()
        {
            lState.Text = "NEW";

            dsUsers.Tables["_USER"].Rows.Add();

            gvUser.DataSource = dsUsers;
            gvUser.DataBind();

            gvUser.SelectedIndex = dsUsers.Tables["_USER"].Rows.Count - 1;

            tbUSR_NAME.Text = "";
            tbUSR_LOGIN_NAME.Text = "";
            tbPW.Text = "";
            cbSupervisor.Checked = false;
         //   ddlUSR_FONOK.Visible = true;
            for (int i = 0; i < ddlUSR_FONOK.Items.Count; ++i)
            {
                ddlUSR_FONOK.Items[i].Enabled = (!(ddlUSR_FONOK.Items[i].Value == dsUsers.Tables["_USER"].Rows[gvUser.SelectedIndex]["USR_ID"].ToString()));
                ddlUSR_FONOK.SelectedIndex = 0;
            }
            tbUSR_NAME.Focus();
        }
        protected bool Save(string iMod /*EDIT/NEW*/ , ref string oErr)
        {
            //athelyezve a wfRights formra
            int id;
            string name;
            string login_name;
            int fonok;
            bool supervisor;
            string pw;

            name = tbUSR_NAME.Text;
            login_name = tbUSR_LOGIN_NAME.Text;
            pw = tbPW.Text;

            supervisor = cbSupervisor.Checked;
            if (supervisor)
                fonok = -1;
            else
                fonok = Convert.ToInt32(ddlUSR_FONOK.Items[ddlUSR_FONOK.SelectedIndex].Value);

            lState.Text = "BROWSE";

            if (iMod == "EDIT")
            {
                id = Convert.ToInt32(dsUsers.Tables["_USER"].Rows[gvUser.SelectedIndex]["USR_ID"].ToString());
                return (_clUser.UpdateUsers(id, name, login_name, pw, fonok, supervisor, ref oErr));
            }
            else
            {
                id = Bussines.Utilities.GenId(blObj.sqlConnection);
                return (_clUser.InsertUsers(id, name, login_name, pw, fonok, supervisor, ref oErr));
            }
        }

        private bool Delete(ref string oErr)
        {

            if (dsUsers.Tables["_USER"].Rows[gvUser.SelectedIndex]["USR_SUPERVISOR"].ToString() == "1")
            {
                oErr = "Supervisor nem törölehető!";
                return (false);
            }
            else
            {

                int id;


                id = Convert.ToInt32(dsUsers.Tables["_USER"].Rows[gvUser.SelectedIndex]["USR_ID"].ToString());

                return (_clUser.DeleteUser(id, ref oErr));

            }
        }
        #endregion

        #region Események a gombokhoz, gridben lépegetéshez

        protected void bSave_Click(object sender, EventArgs e)
        {
            string Err = "";
            bool jo = true;
            if (lState.Text == "NEW")
                jo = Save("NEW", ref Err);
            else
                jo = Save("EDIT", ref Err);
            if (jo)
            {
                int si = gvUser.SelectedIndex;

                DataInit();

                gvUser.SelectedIndex = si;

                gvUser_SelectedIndexChanged(sender, e);
                lMessage.Text = "";
            }
            else
            {
                lMessage.Text = "Hiba a mentés közben : " + Err;
            }

        }

        protected void bDelete_Click(object sender, EventArgs e)
        {
            string Err = "";
            if (dsUsers.Tables["_USER"].Rows[gvUser.SelectedIndex]["USR_SUPERVISOR"].ToString() == "1")
            {
                lMessage.Text = "Supervisor nem lehet inaktív!";
            }
            else
            {
                int id;

                id = Convert.ToInt32(dsUsers.Tables["_USER"].Rows[gvUser.SelectedIndex]["USR_ID"].ToString());

                if (_clUser.AktivUser(id, ref Err))
                {
                    DataInit();

                    gvUser.SelectedIndex = 1;

                    gvUser_SelectedIndexChanged(sender, e);
                    lMessage.Text = "";
                }
                else
                {
                    lMessage.Text = "Hiba a művelet közben : " + Err;
                }

            }

          /*  string Err = "";
            if (Delete(ref Err))
            {
                DataInit();

                gvUser.SelectedIndex = 1;

                gvUser_SelectedIndexChanged(sender, e);
                lMessage.Text = "";
            }
            else
            {
                lMessage.Text = "Hiba a törlés közben : " + Err;
            }
            */
        }

        protected void bUj_Click(object sender, EventArgs e)
        {
            //Uj();
            Session["MOD_USR_ID"] = -1; //(int)dsUsers.Tables["_USER"].Rows[gvUser.SelectedIndex]["USR_ID"];
            Response.Redirect("wfRights.aspx");
        }

        protected void bCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("wfUsers.aspx");
        }

        protected void gvUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            //lépegetés a gridben
        /*    bool supervisor = false;

            tbUSR_NAME.Text = dsUsers.Tables["_USER"].Rows[gvUser.SelectedIndex]["USR_NAME"].ToString();
            tbUSR_LOGIN_NAME.Text = dsUsers.Tables["_USER"].Rows[gvUser.SelectedIndex]["USR_LOGIN_NAME"].ToString();
            tbPW.Text = dsUsers.Tables["_USER"].Rows[gvUser.SelectedIndex]["USR_PW"].ToString();
            supervisor = (dsUsers.Tables["_USER"].Rows[gvUser.SelectedIndex]["USR_SUPERVISOR"].ToString() == "1");
            cbSupervisor.Checked = supervisor;
         //   ddlUSR_FONOK.Visible = (!(supervisor));
            for (int i = 0; i < ddlUSR_FONOK.Items.Count; ++i)
            {
                ddlUSR_FONOK.Items[i].Enabled = (!(ddlUSR_FONOK.Items[i].Value == dsUsers.Tables["_USER"].Rows[gvUser.SelectedIndex]["USR_ID"].ToString()));
                if (ddlUSR_FONOK.Items[i].Value == dsUsers.Tables["_USER"].Rows[gvUser.SelectedIndex]["USR_FONOK"].ToString())
                    ddlUSR_FONOK.SelectedIndex = i;
            }*/
            lMessage.Text = "";
        }
        #endregion

        protected void bRights_Click(object sender, EventArgs e)
        {
            Session["MOD_USR_ID"] = (int)dsUsers.Tables["_USER"].Rows[gvUser.SelectedIndex]["USR_ID"];
            Response.Redirect("wfRights.aspx");
        }

        protected void bUnlock_Click(object sender, EventArgs e)
        {
            _clUser.UnLockUser((int)dsUsers.Tables["_USER"].Rows[gvUser.SelectedIndex]["USR_ID"]);
        }

        protected void bVissza_Click(object sender, EventArgs e)
        {            
            Response.Redirect("wfDefault.aspx");
        }

        protected void gvUser_RowClick(object sender, CustomControls.Grid.RowClickEventArgs e)
        {
            gvUser_SelectedIndexChanged(sender, e);
        }

        protected void gvUser_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.ID = e.Row.Cells[0].Text;
                //e.Row.Attributes.Add("onclick", "GridView_selectRow(this,'" + e.Row.Cells[1].Text + "')");
                e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.gvUser, "Select$" + e.Row.RowIndex);
                //e.Row.Attributes.Add("onclick", "GridView_selectRow(this,'" + e.Row.Cells[0].Text + "')");
                e.Row.Attributes.Add("onmouseover", "GridView_mouseHover(this)");
            }

            
        }

   
    }
}
